package com.letv.android.client.album.controller;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.letv.android.young.client.R;
import com.letv.android.client.activity.AlbumPlayRoomActivity;
import com.letv.android.client.controller.ScreenObservable;
import com.letv.core.bean.HomeMetaData;
import com.letv.core.download.image.ImageDownloader;
import com.letv.core.utils.LogInfo;
import com.letv.core.utils.NetworkUtils;
import com.letv.core.utils.StatisticsUtils;
import com.letv.core.utils.UIsUtils;
import com.letv.datastatistics.util.PageIdConstant;

import java.util.Observable;
import java.util.Observer;

/**
 * 联播推荐提示控制器
 * 
 * @author zhuqiao
 *
 */
public class AlbumRoomRecommendController implements Observer {
    /**
     * 播放结束前显示RE推荐影片的时间(s)
     */
    private final int SHOW_PLAY_RECOMMEND_TIP_TIME = 20;
    /**
     * 显示推荐影片持久时间
     */
    private final int SHOW_PLAY_RECOMMEND_TIP_TIME_CONTINUE = 8;
    /**
     * 8s隐藏
     */
    private static final int DELAYCLOSE = 8 * 1000;
    private final int MSG = 1;

    private AlbumPlayRoomActivity mActivity;

    // private View mParentView;
    private View mContainView;
    private View mImageFrameView;
    private ImageView mRecommendImageView;
    private View mClose;
    private TextView mContentTextView, mHalfContentTextView;

    /**
     * 推荐Tip是否关闭
     */
    private boolean mIsCloseVideoRecommend = false;
    private boolean mHasShowed = false;

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG:
                    mHasShowed = true;
                    hideRecommendTipView();
                    break;
                default:
                    break;
            }
        }

    };

    public AlbumRoomRecommendController(AlbumPlayRoomActivity activity) {
        mActivity = activity;

        init();
    }

    private void init() {
        // mParentView = mActivity.findViewById(R.id.album_recommend_frame);
        mContainView = mActivity.findViewById(R.id.detailplay_full_recommend_tip);
        mImageFrameView = mActivity.findViewById(R.id.recommend_frame);
        mRecommendImageView = (ImageView) mActivity.findViewById(R.id.imagev_recommend);
        mClose = mActivity.findViewById(R.id.imagev_recommend_close);
        mContentTextView = (TextView) mActivity.findViewById(R.id.textv_recommend_content);
        mHalfContentTextView = (TextView) mActivity.findViewById(R.id.textv_recommend_content_half);

        mClose.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mIsCloseVideoRecommend = true;
                mHandler.removeMessages(MSG);
                hideRecommendTipView();
            }
        });
    }

    /**
     * 显示
     * 
     * @param curTime
     * @param endTime
     */
    public void showPlayRecommendTip(long curTime, long endTime) {
        if (mContainView.getVisibility() == View.VISIBLE || mHasShowed) {
            return;
        }

        // 视频播放结束前20秒时（客户端可配置），出现联播提示，采取大TIP样式，大Tip显示8S
        long curTimeTmp = curTime / 1000 + SHOW_PLAY_RECOMMEND_TIP_TIME;
        long endTimeBefore8Sec = endTime - SHOW_PLAY_RECOMMEND_TIP_TIME_CONTINUE;
        // LogInfo.log("zhuqiao", "curTime:" + (curTime / 1000) + "..endTimeBefore8Sec:" + endTimeBefore8Sec
        // + "..curTimeTmp:" + curTimeTmp + "..endTime:" + endTime);
        if (curTime / 1000 <= endTimeBefore8Sec && curTimeTmp >= endTime && !mIsCloseVideoRecommend) {
            // 跳过片尾前SHOW_PLAY_RECOMMEND_TIP_TIME, 显示推荐影片tip
            // ADD 8S自动隐藏
            // LogInfo.log("zhuqiao", "进入showRecommendTipView recommend:"
            // + (mActivity.getFlow().getVideoRecommend() != null));
            mHandler.removeMessages(MSG);
//            showRecommendTipView();
            mHandler.sendEmptyMessageDelayed(MSG, DELAYCLOSE);
        } else {
            mHandler.removeMessages(MSG);
            hideRecommendTipView();
        }
    }

    /**
     * 显示推荐影片tip
     */
    private void showRecommendTipView() {
        /*
         * 产品盖棚需求变更，从轮播台进入半屏播放页也需要续播
         */
        if (isControllerShowing() || mActivity.getFlow() == null || mActivity.getFlow().getVideoRecommend() == null
                || !NetworkUtils.isNetworkAvailable(mActivity)) {
            hideRecommendTipView();
            return;
        }

        HomeMetaData recommend = mActivity.getFlow().getVideoRecommend();

        mContainView.setVisibility(View.VISIBLE);
        ImageDownloader.getInstance(mActivity).download(mRecommendImageView, recommend.getMobilePic());

        if (!TextUtils.isEmpty(recommend.getNameCn())) {
            mContentTextView.setText(recommend.getNameCn());
            mHalfContentTextView.setText(recommend.getNameCn());
        } else {
            mContentTextView.setText(recommend.getSubTitle());
            mHalfContentTextView.setText(recommend.getSubTitle());
        }

        // 5.7需求
        LogInfo.log("zhuqiao", "大tip曝光");
        StatisticsUtils.staticticsInfoPostAddTargetUrl(mActivity, "19", "c68", "1008", 3, "picture", null, null, null,
                null, PageIdConstant.fullPlayPage, null, null, null, null, null, null, null);
    }

    public void hideRecommendTipView() {
        mContainView.setVisibility(View.GONE);
    }

    public boolean isControllerShowing() {
        if (UIsUtils.isLandscape(mActivity)) {
            if (mActivity.getFullControllerFragment() != null) {
                return mActivity.getFullControllerFragment().isVisible();
            }
        } else {
            if (mActivity.getHalfControllerFragment() != null) {
                return mActivity.getHalfControllerFragment().isVisible();
            }
        }

        return false;
    }

    public void reset() {
        mHasShowed = false;
    }

    @Override
    public void update(Observable observable, Object data) {
        if (data instanceof String) {
            String notify = (String) data;
            if (TextUtils.equals(ScreenObservable.ON_CONFIG_CHANGE, notify)) {
                if (UIsUtils.isLandscape(mActivity)) {
                    mContainView.getLayoutParams().width = UIsUtils.dipToPx(mActivity,292);
                    mContainView.getLayoutParams().height = UIsUtils.dipToPx(mActivity,80);
                    mImageFrameView.getLayoutParams().width = UIsUtils.dipToPx(mActivity,120);
                    mImageFrameView.getLayoutParams().height = UIsUtils.dipToPx(mActivity,68);
                    mContentTextView.setVisibility(View.VISIBLE);
                    mHalfContentTextView.setVisibility(View.GONE);
                } else {
                    mContainView.getLayoutParams().width = UIsUtils.dipToPx(mActivity,226);
                    mContainView.getLayoutParams().height = UIsUtils.dipToPx(mActivity,60);
                    mImageFrameView.getLayoutParams().width = UIsUtils.dipToPx(mActivity,90);
                    mImageFrameView.getLayoutParams().height = UIsUtils.dipToPx(mActivity,50);
                    mContentTextView.setVisibility(View.GONE);
                    mHalfContentTextView.setVisibility(View.VISIBLE);
                }
            }
        }
    }

}
